package com.airspyar;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
	private Camera camera=null;
	private SurfaceHolder holder=null;
	private int width=0, height=0;
	private boolean active=true;

	public CameraView(Context context, AttributeSet attrs) {
	    super(context, attrs);
	    holder = this.getHolder();
	    holder.addCallback(this);
	    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public boolean isPreviewActive(){
	    return active;
	}

	public void show(){
	    active=true;
	    startPreview();
	}
	
	public void hide(){
	    active=false;
		stopPreview();
	}

	private void startPreview(){		
	    if(camera==null || !(width>0 && height>0))
	        return;
	    
	    Camera.Parameters parameters = camera.getParameters();
	    Size size=getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), width, height);
	    parameters.setPreviewSize(size.width, size.height);

	    camera.setParameters(parameters);
	    camera.startPreview();
	    
		this.setBackgroundColor(Color.TRANSPARENT);
	}
	
	private void stopPreview(){
		this.setBackgroundColor(Color.argb(0xff, 0x26, 0x29, 0x2c));
		
		if(camera!=null)
	        camera.stopPreview();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(camera==null){
		    camera=Camera.open();
	        camera.setPreviewCallback(null);
		}
		
	    try {
	        camera.setPreviewDisplay(holder);
	    }catch (IOException e) {
	    	if(AirSpyActivity.LOG_ERROR)
	    		Log.e("Preview Exception", e.getMessage());
	    }
	    
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		width=0;
		height=0;
		
	    if(camera!=null){
	        camera.stopPreview();
	        camera.release();
	        camera=null;
	    }
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		width=w;
		height=h;
		
		if(active)
			startPreview();
	}
	
    private static Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

}
